package org.dtrd.modules.evaluation.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.entity.bean.CdmsesDetail;
import org.dtrd.modules.evaluation.entity.bean.CdmsesListInfo;
import org.dtrd.modules.evaluation.entity.request.CdmsesListRequest;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdCdmsesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 自我管理效能（C-DMSES） 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-15
 */
@Api(tags = "量表评估")
@RestController
@RequestMapping("/evaluation/cdmses/")
public class CdmsesController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdCdmsesService cdmsesService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation(value = "查询最新的自我管理效能（C-DMSES）详情", response = CdmsesDetail.class)
    @GetMapping("get")
    public Result<?> getCdmsesLatest(@RequestParam Integer patientId){
        CdmsesDetail info = cdmsesService.getCdmsesLatest(patientId);
        String logContent = String.format("查询自我管理效能（C-DMSES）详情，查询id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(info);
    }

    @ApiOperation(value = "查询自我管理效能（C-DMSES）列表", response = CdmsesListInfo.class)
    @PostMapping("list")
    @AutoLog(value="查询自我管理效能（C-DMSES）列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getCdmsesList(@RequestBody CdmsesListRequest request){
        List<CdmsesListInfo> list = cdmsesService.getCdmsesList(request);
        String logContent = String.format("查询自我管理效能（C-DMSES）列表, 查询条件: %s", request.toString());
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(list);
    }

    @ApiOperation("新增自我管理效能（C-DMSES）")
    @PostMapping("edit")
    public Result<?> saveOrUpdateCdmses(@RequestBody CdmsesDetail dto){
        CdmsesDetail info = cdmsesService.saveOrUpdateCdmses(dto);
        String logContent = String.format("新增自我管理效能（C-DMSES）, 新增用户id: %d", dto.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("删除自我管理效能（C-DMSES）")
    @DeleteMapping("remove")
    @AutoLog(value="删除自我管理效能（C-DMSES）", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeCdmses(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = cdmsesService.removeCdmses(id);
        String logContent = String.format("删除自我管理效能（C-DMSES）, 删除记录id: %d", id);
        baseCommonService.addLog(logContent, OPERATE_TYPE_DELETE);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
